dofile('src/print_r.lua')

--local Button = require('src/Widgets/Button')
--local Class = require('leviathan/Class')
local Color = require('libs/colorlibrary/colorlibrary')
local Core = require('src/Core')
--local Event = require('src/Event')
local Graphics = require('src/graphics')
local i18n = require('libs/i18n')
local Label = require('src/Widgets/Label')
local Log = require('src/Log')
local Picture = require('src/Widgets/Picture')
local Snow = require('libs/love-snowflakes/snow')
local State = require('src/State')
local Tween = require('libs/tween/tween')
--local Widget = require('src/Widgets/Widget')

local screenState = State:derive('Screen')

function screenState:new(name)
    Log:info('ScreenState. Creating new screen: '..name)
--    screenState.super.new(self)
    self.name = name
    -- get data
    local s = base.screens[name]
    self.image = s.image
    self.duration = s.duration
    self.text = s.text
    self.following = s.following
    self.skip = s.skip
    self.music = s.music
    self.fadein = s.fadein
    self.fadeout = s.fadeout
    -- screen dimensions
    local sw, sh = love.window.getMode()

    -- background
    local bgImage = love.graphics.newImage(self.image)
    self.bg = Picture(0, 0, sw, sh, bgImage, Color.white, 'center', 'center', true)

--     -- TODO переделать на более общий шрифт?
    if self.text then
        local wd = sw / 10 -- x offset is 10 % of screen width
        local lw = sw - 2 * sw / 10
        local f = g_fontTypeWriterMedium
--        local f = g_fontTypeWriterSmall
        love.graphics.setFont(f)
        self.FontWidth = f:getWidth('.')
        self.FontHeight = f:getHeight('.')
        local _, lines = f:getWrap(self.text, lw) -- get number of lines for text
        local l = #lines + 1 -- for empty line below
        self.label = Label(wd, sh - l * self.FontHeight, lw, nil, self.text, f, Color.white, 'center', 'center', Color.black)
        self.bg:addChild(self.label)
    end
    self.status = 'created'
end

function screenState:load()
    if self.fadein then
        print('FADEIN')
--        local s = ENGINE.SM:initState('FadeState', 'fadein')
--        s.fadein = true
--        s.fadeout = false
        ENGINE.SM:InitState('FadeState', 'FadeState')
        ENGINE.SM:Switch('FadeState', true)
        print('FADEIN inited')
    end
    self.timeStart = love.timer.getTime()
    -- events
print('HOOK')
    ENGINE.EVENTS:hook('key_pressed', self.skipScreen)
end

function screenState:update(dt)
-- self.cover:update(dt)
    local t = love.timer.getTime()
--   print(t, self.timeStart, self.duration)
    if t - self.timeStart > self.duration then
        --Core.walk(self.following)
        ENGINE.SM:PopAll()
        ENGINE.CORE:walk(self.following)
    end
end

function screenState:draw()
    -- draw background
    love.graphics.setColor(0, 0, 0, 1)
    love.graphics.rectangle('fill', 0, 0, love.graphics.getWidth(), love.graphics.getHeight())
    self.bg:draw()
--    self.label:draw()
    Graphics:ShowFPS()
end


function screenState:exit()
print('UNHOOK')
    ENGINE.EVENTS:unhook('key_pressed', self.skipScreen)
--    ENGINE.EVENTS:remove('key_pressed')
end


function screenState:skipScreen()
    ENGINE.SM:PopAll()
    -- get skip to
    local here = Core.here()
    local loc = Core.getReference(here)
--    Core.walk(loc.skip)
    ENGINE.CORE:walk(loc.skip)
end

-- ----------

return screenState
